//
//  EMLoginInputView.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/7.
//  Copyright © 2020 zh. All rights reserved.
//

#import "EMLoginInputView.h"
#import "EMSMSCodeView.h"

@interface EMLoginInputView () <EMSMSCodeViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) MASConstraint *smsWidth;

@end

@implementation EMLoginInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *break1 = [[UIView alloc] init];
        break1.backgroundColor = ColorFromRGB(0xeeeeee);
        [self addSubview:break1];
        [break1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.height.mas_equalTo(0.5);
        }];
        
        UIView *break2 = [[UIView alloc] init];
        break2.backgroundColor = ColorFromRGB(0xeeeeee);
        [self addSubview:break2];
        [break2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self);
            make.height.mas_equalTo(0.5);
        }];
        
        _textField = [[UITextField alloc] init];
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.font = FONT_PINGFANG_REGULAR_(16);
        _textField.textColor = ColorFromRGB(0x333333);
//        _textField.placeholder = NSLocalizedString(@"请输入手机号", nil);
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"请输入手机号", nil) attributes:@{NSFontAttributeName:FONT_PINGFANG_REGULAR_(16),NSForegroundColorAttributeName:ColorFromRGB(0xc2c2c2)}];
        _textField.attributedPlaceholder = attr;
        [_textField addTarget:self action:@selector(textFieldChanged) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_textField];
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(6);
            make.centerY.equalTo(self);
        }];
        
        _smsCodeView = [[EMSMSCodeView alloc] init];
        _smsCodeView.delegate = self;
        [self addSubview:_smsCodeView];
        [_smsCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.mas_equalTo(-20);
            make.left.equalTo(_textField.mas_right);
            self.smsWidth = make.width.mas_equalTo(0);
        }];
    }
    return self;
}

- (NSString *)currentInputString
{
    return self.textField.text;
}

- (void)textFieldChanged {
    if (_delegate) {
        [_delegate inputView:self textfieldChanged:_textField];
    }
}

- (void)setText:(NSString *)text
{
    _textField.text = text;
}

- (void)showVerifyCode
{
    self.textField.placeholder = NSLocalizedString(@"请输入验证码", nil);
    self.smsWidth.offset(140);
    [self layoutIfNeeded];
    [self.delegate inputView:self clickSmsBtn:_smsCodeView.smsBtn inSMSCodeView:_smsCodeView];
}

#pragma mark - EMSMSCodeViewDelegate

- (void)smsCodeView:(EMSMSCodeView *)smsCodeView clickSmsBtn:(UIButton *)smsBtn
{
    [self.delegate inputView:self clickSmsBtn:smsBtn inSMSCodeView:smsCodeView];
}

@end
